package com.zxh.blog.web;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zxh.blog.domain.Permission;
import com.zxh.blog.domain.Role;
import com.zxh.blog.service.PermissionService;
import com.zxh.blog.service.RoleService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.*;


@Controller
@RequestMapping("/role")
public class AdminRoleController {

	@Autowired
	RoleService roleService;

	@Autowired
	PermissionService permissionService;

	@RequiresPermissions("role:read")
	@RequestMapping(value = "/index",method = RequestMethod.GET)
	public String index(){
		return "/role/index.jsp";
	}

	@RequiresPermissions("role:add")
	@RequestMapping(value = "/create",method = RequestMethod.POST)
	@ResponseBody
	public Map create(Role role){
		Date date = new Date();
		role.setCreate_time(date);
		role.setUpdate_time(date);
		Integer integer = roleService.addRole(role);
		HashMap map = new HashMap();
		if(integer == 1){
			map.put("result","success");
		}else{
			map.put("result","fail");
		}
		return map;
	}

	@RequiresPermissions("role:update")
	@RequestMapping(value = "/update",method = RequestMethod.POST)
	@ResponseBody
	public Map update(Role role){
		Date date = new Date();
		role.setUpdate_time(date);
		roleService.updateRole(role);
		HashMap map = new HashMap();
		if(true){
			map.put("result","success");
		}else{
			map.put("result","fail");
		}
		return map;
	}

	@RequiresPermissions("role:dalete")
	@RequestMapping(value = "/delete/{ids}",method = RequestMethod.GET)
	@ResponseBody
	public Map delete(@PathVariable("ids") int[] ids){
		roleService.deleteByIds(ids);
		HashMap map = new HashMap();
		if(true){
			map.put("result","success");
		}else{
			map.put("result","fail");
		}
		return map;
	}

	//@RequestParam(required = false, defaultValue = "0", value = "offset")
	//@RequestParam(required = false, defaultValue = "10", value = "limit")
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	@ResponseBody
	public Map list(@RequestParam int offset,@RequestParam int limit) {
		int a = (offset/limit)+1;
		PageHelper.startPage(a,limit);
		List<Role> roleList = roleService.selectByList();
		PageInfo<Role> p=new PageInfo<Role>(roleList);
		Map<String, Object> result = new HashMap();
		result.put("rows", p.getList());
		result.put("total", p.getTotal());
		return result;
	}

	@RequestMapping(value = "/getPermission",method = RequestMethod.GET)
	@ResponseBody
	public Object getPermission(@RequestParam Integer roleId){
		List<Permission> rolePlist = roleService.findPermissionByRoleId(roleId);
		List<Permission> permissionList =permissionService.selectByList();
		ArrayList node = new ArrayList();
		HashMap result = new HashMap();
//		System.out.println("111111"+rolePlist.toString());
//		System.out.println("222222"+permissionList.toString());
		for(int i = 0;i<permissionList.size();i++){
			HashMap map = new HashMap();
			for(int a = 0;a<rolePlist.size();a++){
				if(permissionList.get(i).getId() == rolePlist.get(a).getId()){
					map.put("checked","true");
				}
			}
			map.put("id",permissionList.get(i).getId());
			map.put("name",permissionList.get(i).getName());
			map.put("pId",permissionList.get(i).getParent_id());

			node.add(i,map);
		}
//		Object json = JSONArray.toJSON(node);
		result.put("code","success");
		result.put("result",node);
		return result;
	}

	@RequestMapping(value = "/uptRolePermission",method = RequestMethod.GET)
	@ResponseBody
	public Map uptRolePermission(@RequestParam int roleId, @RequestParam int[] ids){
		Integer insert = 0;
		roleService.deletePerByRoleId(roleId);
		for(int i = 0;i<ids.length;i++){
			insert = roleService.insertRolePermission(roleId, ids[i]);
		}
		HashMap map = new HashMap();
		map.put("result","success");
		return map;
	}

}
